package jjn.round1;

/**
 * @author Jiang Jining
 * @since 2022-12-12 23:22
 */
public class LeetCode1781_SumOfBeautyOfAllSubstrings {
    public int beautySum(String s) {
        int result = 0;
        for (int i = 0; i < s.length(); i++) {
            int[] cnt = new int[26];
            int max = 0;
            for (int j = i; j < s.length(); j++) {
                int index = s.charAt(j) - 'a';
                cnt[index]++;
                max = Math.max(max, cnt[index]);
                int min = s.length();
                for (int k = 0; k < 26; k++) {
                    if (cnt[k] > 0) {
                        min = Math.min(min, cnt[k]);
                    }
                }
                result += max - min;
            }
        }
        return result;
    }
}
